package ru.arriah.servicedesk.web.leadExecutor.action;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.TaskBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;
import ru.arriah.servicedesk.web.commonaction.exception.PrivilegesException;

public class TaskDetailsAction extends DumbAction {
   protected RequestManagerLocalHome requestManagerHome;
   
   public TaskDetailsAction() throws NamingException {
      super();

      requestManagerHome =
         Utils.getRequestManagerHomeInterface();
   }

   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
     try {                   
       
       int taskId = idVal(request.getParameter("task_id"));
       int userId = Utils.getUserId(request);
       
       RequestManagerLocalObject requestManager = 
         requestManagerHome.create();
                                                    
       TaskBean taskBean = requestManager.selectTask(taskId);
       
       RequestBean requestBean = 
          requestManager.selectRequestByTaskId(taskId);
       
       if (requestBean.getLeadExecutorId() == userId) {          
          request.setAttribute("status", "request_lead_executor");          
       } else if (taskBean.getEmployeeId() == userId) {          
          request.setAttribute("status", "executor");          
       } else 
          throw new PrivilegesException("");
          
       
       request.setAttribute("taskBean", taskBean);       
       requestManager.remove();
       
       return actionMapping.getSuccessTarget();
    } catch (Exception e) {
       handleException(e, "execute");
       throw new InternalException("ErrorMessage.internal");
    }
   }
}